package com.hex2int;

public class Hex2IntDemo {

	/**
	 *
	 * Manager of the low level LibLinphone stuff.<br />
	 * Including:
	 * <ul>
	 * <li>Starting C liblinphone</li>
	 * <li>Reacting to C liblinphone state changes</li>
	 * <li>Calling Linphone android service listener methods</li>
	 * <li>Interacting from Android GUI/service with low level SIP stuff/</li>
	 * </ul>
	 *
	 * Add Service Listener to react to Linphone state changes.
	 *
	 * @author Guillaume Beraudo
	 *
	 */
	public static void main(String[] args) {

		byte[] _b = new byte[]{0, 0, 0, 49};
		
		int hex2int = hex2int(_b, 0);
		System.out.println(hex2int);

		int hex2int2 = hex2int(_b);
		System.out.println(hex2int2);

		byte _bb = 0x07;
		System.out.println(_bb);
	}


	public static int hex2int(byte[] b, int offset) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
			int shift = (4 - 1 - i) * 8;
			value += (b[i + offset] & 0x000000FF) << shift;
		}
		return value;
	}

	public static final int hex2int(byte[] b) {
		// System.out.println(b.length);
		if(b.length != 4) {
			throw new IllegalArgumentException("security length must be 4 . ");
		}
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < 4; i++) {
			sb.append(Integer.toHexString(b[i] & 0xFF));
		}
		int i  = Integer.valueOf(sb.toString(), 16);
		return i;
	}

}
